//by Navy Zhang 710605420@qq.com
package com.tencent.memoryuseLibrary;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Debug;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import static android.content.Context.ACTIVITY_SERVICE;

public class MemoryHelper {
    private  static String TAG = "MemoryHelper";

    private static ActivityManager activityManager = null;
    private static Context m_Context = null;
    private  static  int pss = 0;
    private  static  int pid = 0;
    private  static  int[] memoryPid =null;
    private  static    Timer timer;

    public static void  Init(Context context)
    {
        m_Context = context;
        activityManager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
        pid  = android.os.Process.myPid();
        memoryPid = new int[] { pid};
        if(timer!= null)
        {
            timer.cancel();
            timer = null;
        }
    }

    public static  float GetMemoryXmsTotalUsage()
    {
        //当前分配的总内存
        float totalMemory = (float) (Runtime.getRuntime().totalMemory() );
        return  totalMemory ;

    }
    public static float GetMemoryXmsMax()
    {
        float maxMemory = (float) (Runtime.getRuntime().maxMemory() );
        return  maxMemory ;

    }
    public static float GetMemoryXmsFree()
    {
        //剩余内存
        float freeMemory = (float) (Runtime.getRuntime().freeMemory());
        return  freeMemory ;

    }

    public  static  float GetNativeMaxSize()
    {
        return  Debug.getNativeHeapSize();
    }
    public  static  float GetNativeFreeSize()
    {
        return  Debug.getNativeHeapFreeSize();
    }
    public  static  float GetNativeAllocatedSize()
    {
        return  Debug.getNativeHeapAllocatedSize();
    }

    public  static float GetProcessPss()
    {
        if(timer == null)
        {
            timer=new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    try {
                        Debug.MemoryInfo[] memoryInfo = activityManager
                                .getProcessMemoryInfo(memoryPid);

                      int  pss =  memoryInfo[0].getTotalPss();
                        Message message=new Message();
                        message.what=0;
                        message.arg1 =pss;
                        mHandler.sendMessage(message);
                        Log.i(TAG, "pss: "+pss);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            },0,5000);//每隔5秒使用handler发送一下消息,也就是每隔一秒执行一次,一直重复执行
        }
        return  pss;
    }
    private static Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == 0){
                pss = msg.arg1;
            }
        }
    };


}
